{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type of p1: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "2017-06-19 00:00:00\n",
      "type of p2: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "2017-06-19 09:13:45\n",
      "type of p3: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "2017-06-19 09:13:45\n",
      "type of p4: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "2017-06-19 09:13:45\n",
      "type of p5: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "2017-06-19 09:13:45\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime as dt\n",
    "from datetime import date as d\n",
    "from datetime import timedelta as td\n",
    "import time\n",
    "\n",
    "\n",
    "p1=pd.Timestamp(2017,6,19)\n",
    "p2=pd.Timestamp(dt(2017,6,19,hour=9,minute=13,second=45))\n",
    "p3=pd.Timestamp(\"2017-6-19 9:13:45\")\n",
    "\n",
    "p4=pd.to_datetime(\"2017-6-19 9:13:45\")\n",
    "p5=pd.to_datetime(dt(2017,6,19,hour=9,minute=13,second=45))\n",
    "\n",
    "\n",
    "print(\"type of p1:\",type(p1))\n",
    "print(p1)\n",
    "print(\"type of p2:\",type(p2))\n",
    "print(p2)\n",
    "print(\"type of p3:\",type(p3))\n",
    "print(p3)\n",
    "\n",
    "print(\"type of p4:\",type(p1))\n",
    "print(p4)\n",
    "print(\"type of p5:\",type(p2))\n",
    "print(p5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type of p: <class 'pandas._libs.tslibs.timestamps.Timestamp'>\n",
      "2019-02-21 11:34:34.876785654\n",
      "type of p.to_pydatetime() <class 'datetime.datetime'>\n",
      "2019-02-21 11:34:34.876785\n",
      "type of p.ctime() <class 'str'>\n",
      "Thu Feb 21 11:34:34 2019\n",
      "type of p.strftime() <class 'str'>\n",
      "2019 02 21 11:34:34\n",
      "------------------------\n",
      "2019-02-21\n",
      "11:34:34.876785\n",
      "2019-02-21 11:34:34.876785\n",
      "1550748874876785654\n",
      "微秒 876785\n",
      "纳秒 654\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda3/envs/django/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3291: UserWarning: Discarding nonzero nanoseconds in conversion\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    }
   ],
   "source": [
    "p = pd.Timestamp('2019-02-20 10:10:00')\n",
    "p = pd.Timestamp('2019-02-21 11:34:34.876785654')\n",
    "print('type of p:',type(p))\n",
    "print(p)\n",
    "print('type of p.to_pydatetime()',type(p.to_pydatetime()))\n",
    "print(p.to_pydatetime())\n",
    "print('type of p.ctime()',type(p.ctime()))\n",
    "print(p.ctime())\n",
    "print('type of p.strftime()',type(p.strftime('%Y %m %d %H:%M:%S')))\n",
    "print(p.strftime('%Y %m %d %H:%M:%S'))\n",
    "\n",
    "print('------------------------')\n",
    "\n",
    "print(p.date())\n",
    "print(p.time())\n",
    "print(p.to_pydatetime())\n",
    "print(p.value)\n",
    "# 毫秒millisecond    微秒microsecond    纳秒nanosecond    皮秒picosecond    \n",
    "print('微秒', p.microsecond)\n",
    "print('纳秒', p.nanosecond)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pandas中Timestamp类用法讲解\n",
    "# https://blog.csdn.net/csdn15698845876/article/details/73456967\n",
    "\n",
    "# python 时间模块time，datetime\n",
    "# https://www.cnblogs.com/wj-1314/p/8533137.html\n",
    "\n",
    "# python strftime\n",
    "# http://www.runoob.com/python/att-time-strftime.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "702965 702965\n"
     ]
    }
   ],
   "source": [
    "t = dt.now()\n",
    "t.strftime(\"%Y%m%d%H%M%S\")\n",
    "t.timestamp()  # 保留六位小数0.123456，注意不足六位的情况0.1 \n",
    "str(t.timestamp()).replace('.','')[-5:]\n",
    "\n",
    "microsecond = str(t.timestamp()).split('.')[1]\n",
    "microsecond_fixed_width = str(t.timestamp()).split('.')[1].rjust(6,'0')\n",
    "print(microsecond,microsecond_fixed_width)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# datetime.datetime datetime.date 数据元组\n",
    "# timedelta 数据元组\n",
    "# 格式化字符串<--->数据元组<--->时间戳\n",
    "# datetime — Basic date and time types\n",
    "# https://docs.python.org/3/library/datetime.html\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "2019-04-02\n",
      "2019-03-31\n",
      "time.struct_time(tm_year=2019, tm_mon=4, tm_mday=1, tm_hour=10, tm_min=10, tm_sec=10, tm_wday=0, tm_yday=91, tm_isdst=-1) 1554084610.0 1554084610.0\n",
      "time.struct_time(tm_year=2019, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=91, tm_isdst=-1) 1554048000.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# date and datetime  => timestamp\n",
    "x = dt(2019,4,1,10,10,10)\n",
    "y = d(2019,4,1)\n",
    "print(x.date() is y)\n",
    "print(x.date() == y)\n",
    "print(y + td(days=1))\n",
    "print(y + td(days=-1)) \n",
    "print(x.timetuple(), time.mktime(x.timetuple()) )\n",
    "print(y.timetuple(), time.mktime(y.timetuple()))\n",
    "\n",
    "import time\n",
    "import datetime inport datetime, date, timedelta\n",
    "\n",
    "'''\n",
    "# Datetime -> Date\n",
    "datetime.date()\n",
    "\n",
    "# Timestamp  -> Datetime/Date\n",
    "date.fromtimestamp()\n",
    "datetime.fromtimestamp()\n",
    "\n",
    "# Timestamp -> Time Tuple\n",
    "time.struct_time\n",
    "\n",
    "# Datetime/Date -> Time Tuple\n",
    "date.timetuple()\n",
    "datetime.timetuple()\n",
    "\n",
    "# Time Tuple -> TimeStamp\n",
    "time.mktime(timetuple)\n",
    "\n",
    "# time — Time access and conversions\n",
    "# https://docs.python.org/3/library/time.html#module-time\n",
    "\n",
    "# datetime — Basic date and time types\n",
    "# https://docs.python.org/3/library/datetime.html#time-objects\n",
    "'''\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
